import axios from 'axios';
import {ElMessage} from 'element-plus';

const axiosInstance = axios.create({
    baseURL: 'http://localhost:8088/api',
    timeout: 10000
});


// 删除管理员
axiosInstance.deleteAdmin = async (adminId) => {
    try {
        const response = await axiosInstance.delete(`/admin/delete_admin/${adminId}`);
        ElMessage.success('管理员删除成功');
        return response.data.data; // 返回删除成功的响应
    } catch (error) {
        console.error(error);
        ElMessage.error(`删除管理员 ID ${adminId} 失败`);
        throw error; // 抛出错误
    }
};


// 更新管理员
axiosInstance.updateAdmin = async (adminId, adminDTO) => {
    try {
        const response = await axiosInstance.put(`/admin/update_admin/${adminId}`, adminDTO);
        ElMessage.success('管理员信息更新成功');
        return response.data.data; // 返回更新成功的响应
    } catch (error) {
        console.error(error);
        ElMessage.error(`更新管理员 ID ${adminId} 失败`);
        throw error; // 抛出错误
    }
};

axiosInstance.addAdmin = async (admin) => {
    try {
        const response = await axiosInstance.post('/admin/add_admin', admin);
        ElMessage.success('管理员添加成功');
        return response.data.data; // 返回添加成功的响应
    } catch (error) {
        console.error(error);
        ElMessage.error('添加管理员失败');
        throw error; // 抛出错误
    }
};
// 获取所有用户
axiosInstance.fetchAllAdmins = async () => {
    try {
        const response = await axiosInstance.get('/admin/get_all');
        return response.data.data; // 返回成功获取的管理员列表
    } catch (error) {
        console.error(error);
        ElMessage.error('获取管理员列表失败');
        throw error; // 抛出错误
    }
};
export default axiosInstance;
